package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;

import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.DecisionInfo;
import edu.ccut.saturn.workflow.model.IfInfo;
import edu.ccut.saturn.workflow.model.WorkFlowInfo;

public class ActivateDecisionActivity implements IActivateActivity {

	@SuppressWarnings("unchecked")
	public void activate(IParameterContext parameterContext) throws Exception {
		Map<String, String> customParamMap = (Map<String, String>) parameterContext
		.getParameterValue(4);
		Map<String, String> workFlowParamMap = (Map<String, String>) parameterContext
		.getParameterValue(2);
		
		//准备参数 
		SaturnData workFlowInstance = (SaturnData) parameterContext
				.getParameterValue(0);
		WorkFlowInfo workFlowInfo = (WorkFlowInfo) parameterContext
				.getParameterValue(1);
		SaturnData previousActivityInstance = (SaturnData) parameterContext
				.getParameterValue(3);// 开始活动节点
		//当前活动定义对象
		ActivityInfo currentActivityInfo  = (ActivityInfo) parameterContext
		.getParameterValue(5);
		Connection conn = parameterContext.getConnection();
		
		//准备ｊｅｘｌ表达式容器
		//从数据库读出流程实例的content 
		//SaturnData workFlowInstanceOld=Operation.dataBase().queryByPrimaryKey(Constants.SATURNDATA_WF_PROCESS_INSTANCE, workFlowInstance.getString("process_instance_id"), conn);
		//workFlowInstanceOld.get("content");
		JexlContext context = JexlHelper.createContext();
		Map<String, String> values = context.getVars();
		if (customParamMap != null) {
			values.putAll(customParamMap);
		}
		if(workFlowParamMap!=null){
			values.putAll(workFlowParamMap);
		}
		//获得当前活动数据项并附值
		SaturnData currentActivityInstance = WorkFlowTools.getActivityInstance(workFlowInstance,
				currentActivityInfo,conn );
		//核心操作＝＝＝＝插入路由活动实例
		Operation.dataBase().insert(currentActivityInstance, parameterContext.getConnection());
		//核心操作＝＝＝＝插入流转实例
		SaturnData transitionInstance = WorkFlowTools.getTransitionInstance(conn,
				previousActivityInstance, currentActivityInstance);
		Operation.dataBase().insert(transitionInstance, conn);
		//核心操作＝＝＝＝获取下一活动集合
		ArrayList<ActivityInfo> nextActivityList = new ArrayList<ActivityInfo>();
		DecisionInfo decisionInfo = (DecisionInfo) currentActivityInfo;
		String offsetMode = decisionInfo.getOffsetMode();
		if (offsetMode.equals(Constants.ACTIVITY_DECISION_SINGLE)) {
			//单一分支
			List<IfInfo> ifInfos = decisionInfo.getIfinfos();
			boolean hasTrue = false;
			for (int i = 0; i < ifInfos.size(); i++) {
				IfInfo ifInfo = ifInfos.get(i);
				String expr = ifInfo.getExpr();
				String then = ifInfo.getThen();
				//TODO 单一分支的时候，暂时取第一个判断是真的激活，以后添加按优先级驱动
				//如果都为假，则走default关键字维护的表达式的路径
				if (WorkFlowTools.getExpValue(expr, context)) {
					ActivityInfo nextActivityInfo = workFlowInfo
							.getActivityInfoById(then);
					nextActivityList.add(nextActivityInfo);
					hasTrue = true;
					break;
				}
			}
			//如果没有为真的节点,走default节点
			if(!hasTrue){
				for (int i = 0; i < ifInfos.size(); i++) {
					IfInfo ifInfo = ifInfos.get(i);
					String expr = ifInfo.getExpr();
					String then = ifInfo.getThen();
					//TODO 单一分支的时候，暂时取第一个判断是真的激活，以后添加按优先级驱动
					//如果都为假，则走default关键字维护的表达式的路径
					if (Constants.DECISION_DEFAULT.equals(expr)) {
						ActivityInfo nextActivityInfo = workFlowInfo
								.getActivityInfoById(then);
						nextActivityList.add(nextActivityInfo);
						break;
					}
				}
			}
		} else if (offsetMode.equals(Constants.ACTIVITY_DECISION_ALL)) {
			//全部分支
			List<IfInfo> ifInfos = decisionInfo.getIfinfos();
			for (int i = 0; i < ifInfos.size(); i++) {
				IfInfo ifInfo = ifInfos.get(i);
				String then = ifInfo.getThen();
				ActivityInfo nextActivityInfo = workFlowInfo
						.getActivityInfoById(then);
				nextActivityList.add(nextActivityInfo);
			}
		} else {
			//多路分支
			//1、将所有表达式为真后驱活动驱动
			List<IfInfo> ifInfos = decisionInfo.getIfinfos();
			for (int i = 0; i < ifInfos.size(); i++) {
				IfInfo ifInfo = ifInfos.get(i);
				String expr = ifInfo.getExpr();
				String then = ifInfo.getThen();
				if (WorkFlowTools.getExpValue(expr, context)) {
					ActivityInfo nextActivityInfo = workFlowInfo
							.getActivityInfoById(then);
					nextActivityList.add(nextActivityInfo);
				}
			}
			//2、default关键字维护的表达式的路径
			for (int i = 0; i < ifInfos.size(); i++) {
				IfInfo ifInfo = ifInfos.get(i);
				String expr = ifInfo.getExpr();
				String then = ifInfo.getThen();
				if (Constants.DECISION_DEFAULT.equals(expr)) {
					ActivityInfo nextActivityInfo = workFlowInfo
							.getActivityInfoById(then);
					nextActivityList.add(nextActivityInfo);
					break;
				}
			}
		}
		//核心操作＝＝＝＝判断并激活活动
		for(ActivityInfo nextActivityInfo :nextActivityList){
			parameterContext.setParameter(3, currentActivityInstance);//将当前活动替换为下一活动的前一活动
			parameterContext.setParameter(5, nextActivityInfo);
			WorkFlowTools.checkToActivateActivity(parameterContext);
		}
	}
	
}
